<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<meta http-equiv="pragma" content="no-cache">
		<script type="text/javascript" src="js/tools/jquery-1.4.2.min.js"></script>
		<script type="text/javascript" src="js/tools/easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
		<script type="text/javascript" src="js/tools/uploadFile.js"></script>
<script charset="utf-8" src="js/tools/editor/kindeditor-min.js"></script>
<script type="text/javascript" src="js/tools/jqueryurl.js"></script>
<script type="text/javascript" src="js/tools/public.js"></script>
<script charset="utf-8" src="admin/js/public.js"></script>
<style>
table {font-size: 12px;}
table tr {background-color: #F1F6F6;}
table tr td {border: 1px solid #D9D9D9;color: black;padding: 2px;}
</style>
<script type="text/javascript">
var user='<s:property value="#session.user"/>';
KE.show({
				id : 'content',
				resizeMode : 1,
				imageUploadJson : 'image!uploadImageForEditor.action',
				directory:"uploadfile/business",
				width:800,
				height:400
			});
var map;
var marker=null;
function initialize(){
         var lat='<s:property value="business.lat"/>';
		 var lng='<s:property value="business.lng"/>';
		 if(lat==''||lng=='')
		 {
		    lat='36.73888412439431';
		    lng='97.14111328125';
		 }
    var myLatlng =new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
    var myOptions = {
        zoom: 4,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    google.maps.event.addListener(map,'click',function(event){
       $("#lng").val(event.latLng.lng());
       $("#lat").val(event.latLng.lat());
       if(marker==null)
			{
				 marker = new google.maps.Marker({       
			position: event.latLng,        
			map: map});
			}else
			{
				 marker.setPosition(event.latLng);
			}
    });
    
    var list='<s:property value="list"/>'
	if(list!="")
	{
	   var provinceId='<s:property value="list[0][1].id"/>';
	   var cityId='<s:property value="list[0][2].id"/>';
	   var countiesId='<s:property value="list[0][3].id"/>';
	   var loadId='<s:property value="list[0][4].id"/>';
	    provinceList("provinceId",function(){
	         $("#provinceId").val(provinceId);
	     });
	     cityList("cityId",provinceId,function(){
	         $("#cityId").val(cityId);
	     });
	     countiesList("countiesId",cityId,function(){
	        $("#countiesId").val(countiesId);
	     })
	     loadsList("loadId",provinceId,cityId,countiesId,function(){
	        $("#loadId").val(loadId);
	     })
	     
	     var latlng=new google.maps.LatLng(<s:property value="list[0][0].lat"/>0, <s:property value="list[0][0].lng"/>0);
	     map.setCenter(latlng);
		 map.setZoom(15);
		 marker = new google.maps.Marker({       
			position: latlng,        
			map: map});
			
	}else
	{
		var location='<s:property value="#session.location"/>';
		if(location!="")
		{
		   location=location.split(",");
		    provinceList("provinceId",function(){
	         $("#provinceId").val(location[0]);
		     });
		     cityList("cityId",provinceId,function(){
		         $("#cityId").val(location[1]);
		     });
		     countiesList("countiesId",cityId,function(){
		        $("#countiesId").val(location[2]);
		     })
		     loadsList("loadId",provinceId,cityId,countiesId,function(){
		        $("#loadId").val(location[3]);
		         moveMap($("#countiesId").find('option:selected').text(),12);
		     })
		}else
		  {
		     provinceList("provinceId");
		  }
	}
}

//移动地图
function moveMap(keyword,level)
{
   
   var geocoder = new google.maps.Geocoder();
			geocoder.geocode( { 'address': keyword}, function(results, status) {
		      if (status == google.maps.GeocoderStatus.OK) {
		        map.setCenter(results[0].geometry.location);
				map.setZoom(level);
		      } 
		    });
}
function check()
{

   $("#name,#categoryId").css("border","");
   var name=$.trim($("#name").val());
   var loadId=$("#loadId").val();
   var subCategoryId=$("#subCategoryId").val();
   var content=$.trim($("#content").val());
   var image=$("#image").val();
   if(name.length<1||name.length>30)
   {
     $("#name").css({"border": "2px solid red"});
      alert("名称的长度为1~30");
      return false;
   }
   if(loadId==''||loadId==-1)
   {
      alert("请选择所在路名!");
      $("#loadId").css({"border": "2px solid red"});
      return false;
   }
   if(image=="")
   {
     alert("请上传图片!");
     return false;
   }
   if(content.length<1||content.length>5000)
   {
      alert("内容的长度在1~5000");
      return false;
   }
   
  return true;
};
//上传图片
function uploadImage(obj)
{
	var file=$("#file").val();
	if(file=="")
	{
		alert("选择图片");
		return false;
	}
          $.upload("image!uploadImage.action", {
              "directory": "uploadfile/business",
			  "base":  $("#base").val(),
              "image": $("#file")
          }, function (data) {
             if(data.info!="error")
             {
                $("#showImg").remove();
			 	$("#image").val(data.info);
			 	$(obj).parent().append("<a id='showImg' href='"+data.info+"' target='_blank'><img  width='30' height='30' src='"+data.info+"'/></a>");
				alert("上传成功");
             }
          });
 }
</script>
	</head>
	<body onload="initialize()">
	<input  type="hidden" id="base" value="<%=basePath%>">
	<s:actionmessage/>
	<s:if test="%{business == null}">
	    <form action="business!addBusiness.action" method="post" onsubmit="return check();">
	</s:if>
	<s:else>
	     <form action="business!modifyBusiness.action" method="post" onsubmit="return check();">
	     <input type="hidden" name="business.id" value="<s:property value="business.id"/>">
	</s:else>
	 <input type="hidden" name="columnId" id="columnId" value="">
	 <input type="hidden" name="business.image" id="image" value="<s:property value="list[0][0].image"/>">
		<table  style="width: 98%;" align="center">
			<tr><td width="10%">商家名称:</td><td><input type="text" name="business.name" id="name" value="<s:property value="list[0][0].name"/>" maxlength="50"/><font color=red>*必填</font></td></tr>
			<tr><td>所在地址:</td><td>省：<select id="provinceId" name="provinceId" onchange="cityList('cityId',$(this).val(),function(){moveMap($('select[name=loads.provinceId] option[selected]').text(),8);})" style="width:100px;"></select>
			市：<select id="cityId" name="cityId" onchange="countiesList('countiesId',$(this).val(),function(){moveMap($('select[name=loads.provinceId] option[selected]').text()+$('select[name=loads.cityId] option[selected]').text(),11);})" style="width:100px;"></select>
			区（县）：<select id="countiesId" name="countiesId" onchange="loadsList('loadId',$('#provinceId').val(),$('#cityId').val(),$('#countiesId').val(),function(){moveMap($('select[name=loads.provinceId] option[selected]').text()+$('select[name=loads.cityId] option[selected]').text()+$('select[name=loads.countiesId] option[selected]').text(),12);})" style="width:100px;"></select>
			路:<select id="loadId" name="business.loadId" onchange="moveMap($('select[name=loads.provinceId] option[selected]').text()+$('select[name=loads.cityId] option[selected]').text()+$('select[name=loads.countiesId] option[selected]').text()+$('select[name=business.loadId] option[selected]').text(),15)" style="width:100px;"></select></td></tr>
			<tr><td>经纬度：</td><td><input type="text" id="lng" name="business.lng"  value="<s:property value="list[0][0].lng"/>"/><input type="text" id="lat" name="business.lat" value="<s:property value="list[0][0].lat"/>"/></td></tr>
			<tr><td>地图：</td><td><div style="width:450px;height:300px;" id="map_canvas"></div></td></tr>
			<tr><td>图片:</td><td><input type="file" name="file" id="file" /><a href="javascript:void(0);" onclick="uploadImage(this)">上传</a></td></tr>
			<tr><td>商家介绍:</td><td><div><textarea name="business.content" id="content" rows="" cols=""><s:property value="list[0][0].content"/></textarea></div></td></tr>
			<tr><td><input type="submit"  value="保存"/></td><td><input type="button" onclick="window.location.href='admin/business/businessmanager.jsp?columnId='+$.query.get('columnId')" value="返回列表"/></td></tr>
		</table>
	</form>
	</body>
</html>
